package fr.uvsq.isty.bdh.hchess.gui.game;

import fr.uvsq.isty.bdh.hchess.engine.GameData;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

/**
 *
 * @author Florent Hemmi <florent.hemmi@gmail.com>
 */
public class ReplayToolBar extends JToolBar implements Observer {
    
    /** Directory where to find the icons for the toolbar. */
    private static final String ICONS_PATH = "icons/";
    
    private GameData gameData;
    
    private JButton buttonBegin;
    private JButton buttonPrev;
    private JButton buttonNext;
    private JButton buttonEnd;
    private JButton buttonPlay;
    
    /** Default constructor.
     * @param gameData take the gameData for replay interaction
     */
    public ReplayToolBar(GameData gameData) {
        super();
        this.gameData = gameData;
        
        buttonBegin = new JButton(new ImageIcon(getClass().getClassLoader()
                                .getResource(ICONS_PATH+"arrow-left-double.png")));
        buttonBegin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ReplayToolBar.this.gameData.replay_rewind();
            }
        });
        buttonPrev = new JButton(new ImageIcon(getClass().getClassLoader()
                                .getResource(ICONS_PATH+"arrow-left.png")));
        buttonPrev.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ReplayToolBar.this.gameData.replay_oneBackward();
            }
        });
        buttonNext = new JButton(new ImageIcon(getClass().getClassLoader()
                                .getResource(ICONS_PATH+"arrow-right.png")));
        buttonNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ReplayToolBar.this.gameData.replay_goOneForward();
            }
        });
        buttonEnd = new JButton(new ImageIcon(getClass().getClassLoader()
                                .getResource(ICONS_PATH+"arrow-right-double.png")));
        buttonEnd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ReplayToolBar.this.gameData.replay_goToEnd();
            }
        });
        buttonPlay = new JButton(new ImageIcon(getClass().getClassLoader()
                                .getResource(ICONS_PATH+"media-playback-start.png")));
        buttonPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ReplayToolBar.this.gameData.replay_switchToPlay();
            }
        });
        
        this.add(buttonBegin);
        this.add(buttonPrev);
        this.add(buttonNext);
        this.add(buttonEnd);
        this.addSeparator();
        this.add(buttonPlay);
    }
    
    /** Updated the button to disable some of this when there are useless. */
    private void updateToolBar() {
        buttonBegin.setEnabled(gameData.replay_canGoBackward());
        buttonPrev.setEnabled(gameData.replay_canGoBackward());
        buttonNext.setEnabled(gameData.replay_CanGoForward());
        buttonEnd.setEnabled(gameData.replay_CanGoForward());
        if (gameData.replay_isGameFinished() && !gameData.replay_CanGoForward()) {
            buttonPlay.setEnabled(false);
        } else {
            buttonPlay.setEnabled(true);
        }
    }

    /** When this class receive an event, button state are updated */
    public void update(Observable o, Object arg) {
        updateToolBar();
    }
}
